from rest_framework.generics import ListAPIView
from rest_framework.filters import OrderingFilter
from drf_haystack.viewsets import HaystackViewSet

from .serializers import SKUSerializer, SKUIndexSerializer
from .serializers import SKU


class SKUListView(ListAPIView):
    """sku列表"""

    # 指定序列化器
    serializer_class = SKUSerializer

    # 指定过滤排序的后端
    filter_backends = [OrderingFilter]
    # filter_backends = (OrderingFilter)

    # 指定过滤排序的字段
    ordering_fields = ('create_time', 'price', 'sales')

    # 指定查询集，因为我们展示商品的列表是分为商品的分类展示的，
    # 如果已经下架不需要展示，所以以上数据需要自己过滤
    def get_queryset(self):
        # 从路径中获取商品列表所要展示的分类
        # category_id = self.kwargs['category_id']
        category_id = self.kwargs.get('category_id')
        return SKU.objects.filter(category_id=category_id, is_launched=True)


class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer